const {getCarDetail, getCarList, newCar, delCar, updateCar, getNoReversedCar, cancelUpdateCar, resetCar} = require("../controller/car");
const {updateCoachUsername, getCoachByCarNumber, cancelReverseCar, resetReverseCar} = require("../controller/coach");
const {SuccessModel, ErrorModel} = require("../model/response");
var express = require('express')
var router = express.Router()

/**
 * 管理原理有操作
 * Cars.vue: /reset, /assigned, /unassigned, /del/:id, /update, /new, /list
 * Vehicle.vue: /list
 */
// 获取所有车辆
router.get('/list', (req, res, next) => {
  const result = getCarList()
  return result.then(rows => {
    res.json(new SuccessModel(rows))
  })
})
// 添加车辆
router.post('/new', (req, res, next) => {
  const result = newCar(req.body)
  return result.then(val => {
    if (val) {
      res.json(new SuccessModel('添加车辆成功'))
    } else {
      res.json(new ErrorModel('添加车辆失败'))
    }
  })
})
// 修改车辆
router.post('/update', (req, res, next) => {
  const result = updateCar(req.body)
  return result.then(val => {
    if (val) {
      res.json(new SuccessModel('更新车辆成功'))
    } else {
      res.json(new ErrorModel('更新车辆失败'))
    }
  })
})
// 删除车辆
router.post('/del/:id', (req, res, next) => {
  const result = delCar(req.params.id)
  return result.then(val => {
    if (val) {
      res.json(new SuccessModel('删除车辆成功'))
    } else {
      res.json(new ErrorModel('删除车辆失败'))
    }
  })
})
// 分配教练
router.post('/assigned', async (req, res, next) => {
  const CarResult = await updateCar(req.body.car)
  if (CarResult) {
    const CoachResult = await updateCoachUsername(req.body.coach_identify, req.body.car.car_number)
    if (CoachResult) {
      res.json(new SuccessModel('车辆分配成功'))
    }
  } else {
    res.json(new ErrorModel('车辆分配失败'))
  }
})
// 取消分配
router.post('/unassigned', async (req, res, next) => {
  const CarResult = await cancelUpdateCar(req.body)
  if (CarResult) {
    const CoachInfo = await getCoachByCarNumber(req.body.car_number)
    if (CoachInfo.identify) {
      const CoachResult = await cancelReverseCar(CoachInfo.identify)
      if (CoachResult) {
        res.json(new SuccessModel('车辆取消分配成功'))
      }
    }
  } else {
    res.json(new ErrorModel('车辆取消分配失败'))
  }
})
// 新的一天来临，完成车辆预约时间重置（完全可以交给数据库进行自动重置）
router.get('/reset', async (req, res, next) => {
  // 1. 获取所有车辆
  const cars = await getCarList();
  // 2. 获取有预约日期的车辆
  const cars_appointe = cars.filter(item => item.appoint_date)
  // 3. 获取预约日期已过的车辆
  const cars_appointed = cars_appointe.filter(item => {
    return new Date(item.appoint_date).getTime() < Date.now()
  })
  // 4. 获取预约日期已过车辆的id
  const arr = cars_appointed.map(item => item.id)
  if (arr.length > 0) {
    //  重置有被预约的车辆
    const val = await resetCar(arr)
    // 重置预约车辆的教练
    if (val) {
      // 重置教练预约车辆
      await resetReverseCar()
      res.json(new SuccessModel())
    } else {
      res.json(new ErrorModel())
    }
  } else {
    res.json(new SuccessModel())
  }
})

/**
 * 教练路由操作
 * CoachCar.vue: /list
 * CarReverse.vue: /not-use, /reset
 */
// 获取未预约车辆信息
router.get('/not-use', (req, res, next) => {
  const result = getNoReversedCar()
  return result.then(rows => {
    res.json(new SuccessModel(rows))
  })
})

// 获取车辆详细信息（该路由未用到）
router.get('/detail', (req, rers, next) => {
  const result = getCarDetail(req.body.car_number)
  return result.then(row => {
    res.json(new SuccessModel(row))
  })
})
module.exports = router
